package br.com.itx.drools.einstein;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.drools.WorkingMemory;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.drools.DroolsHandler;

@Name("einsteinGame")
@Scope(ScopeType.CONVERSATION)
public class EinsteinGame extends DroolsHandler implements Serializable {

	private static final long serialVersionUID = 1L;

	private List<Casa> casas;
	private Set<String> validas;
	private Set<String> invalidas;

	@Create
	@Begin
	public void begin() {
		casas = new ArrayList<Casa>();
		for (int i = 1; i < 6; i++) {
			Casa casa = new Casa();
			casa.setPosicao(i);
			casas.add(casa);
		}
	}

	@Factory(value = "casas")
	public List<Casa> getCasas() {
		return casas;
	}

	public void fireRules() {
		getRegrasValidas().clear();
		getRegrasInvalidas().clear();
		List<String> expressions = new ArrayList<String>();
		expressions.add("#{einsteinGame.casas}");
		WorkingMemory wm = getWorkingMemory("workingMemory", expressions, null,
				null);
		wm.fireAllRules();
	}

	@Factory(value = "validas")
	public Set<String> getRegrasValidas() {
		if (validas == null) {
			validas = new HashSet<String>();
		}
		return validas;
	}

	@Factory(value = "invalidas")
	public Set<String> getRegrasInvalidas() {
		if (invalidas == null) {
			invalidas = new HashSet<String>();
		}
		return invalidas;
	}

}
